<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

use think\facade\Hook;

/**
 * 视频分类标签
 * @package app\video\admin
 */
class Classlabel extends Admin
{
    /**
     * 首页
     * @author shiqiang <976162041@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        
		// 获取筛选
        $map = $this->getMap();
		
		$list = model('VideoPlayClassLabel') -> where($map) -> order('id desc') -> paginate();
		
		//新增
		$btn_add = [
			'title' => '新增',
			'icon'  => 'fa fa-plus-circle',
			'class' => 'btn btn-primary pop',
			'href'  => url('add'),
		];
		
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('标签列表') // 设置页面标题
            ->setTableName('video_play_class_label') // 设置数据表名
			->setSearch(['id' => '标签id', 'title' => '标签名称']) // 设置搜索参数
            ->addColumns([ // 批量添加列
                ['id', 'id'],
                ['title', '标签', 'text.edit'],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter(['report_status']) // 添加筛选
			// ->addTopButtons('add', true) // 批量添加顶部按钮
			->addTopButton('custom', $btn_add, true) // 批量添加顶部按钮
			->addTopButton('delete')
			->addRightButtons('delete') // 批量添加右侧按钮
			->setRowList($list) // 设置表格数据,
            ->fetch(); // 渲染页面
    }
	
	/**
     * 新增
     * @author shiqiang <976162041@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add($pid = ''){  
		// 保存数据
		if ($this->request->isPost()) {
			//数据接收
			$data = $this->request->post('', null, 'trim');
			
			$title = isset($data['title']) ? trim($data['title']) : '';
			
			if ($title == '') {
                $this->error('数据格式错误');
            }
			$title = explode(',', $title);
			
			$list = [];
			foreach($title as $value){
				$map = [];
				$map['title'] = $value;
				$list_class = model('VideoPlayClassLabel') -> where($map) -> find();
				if(empty($list_class)){
					$list[] = ['title' => $value];
				}
			}
			$rel = model('VideoPlayClassLabel') -> addData($list);
			
			if($rel['code'] == 200){
				$this->success('新增成功', null, '_parent_reload');
			}else{
				$this->error($rel['message']);
			}
			//业务逻辑
		}
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增标签')
			->addTags('title', '自定义标签', '多个标签可以用英文 "," 号分割')
            ->setTrigger('auto_create', '1', 'child_node', false)
            ->fetch();
    }
}
